home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / what's new? / sample code / human interface toolbox / packagetool / sample package / htmlsample sources / ciconbuttons.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-02  |  4.3 KB  |  139 lines

  1. /*
  2.     file CIconButtons.c
  3.     
  4.     Description:
  5.     This file contains routines used to implement the color icon buttons
  6.     displayed in the top of HTMLSample's windows.
  7.     
  8.     HTMLSample is an application illustrating how to use the new
  9.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  10.     is Apple's light-weight HTML rendering engine capable of
  11.     displaying HTML files.
  12.  
  13.     Copyright: © 1999 by Apple Computer, Inc.
  14.     all rights reserved.
  15.     
  16.     Disclaimer:
  17.     You may incorporate this sample code into your applications without
  18.     restriction, though the sample code has been provided "AS IS" and the
  19.     responsibility for its operation is 100% yours.  However, what you are
  20.     not permitted to do is to redistribute the source as "DSC Sample Code"
  21.     after having made changes. If you're going to re-distribute the source,
  22.     we require that you make it clear in the source that the code was
  23.     descended from Apple Sample Code, but that you've made changes.
  24.     
  25.     Change History (most recent first):
  26.     10/16/99 created
  27. */
  28.  
  29. #include "CIconButtons.h"
  30. #include <QuickDraw.h>
  31. #include <Icons.h>
  32. #include <Events.h>
  33. #include <Resources.h>
  34. #include <string.h>
  35.  
  36.  
  37.  
  38. /* NewCIconButton retrieves a new color icon button
  39.     resource from the resource file.  the id number
  40.     corresponds to the resource id of the CICB resource. */
  41. CIconButtonHandle NewCIconButton(short id) {
  42.     Handle theRsrc;
  43.     theRsrc = GetResource(kIconButtonType, id);
  44.         /* here, we call detach resource because we
  45.         may have many windows open with many buttons
  46.         each having a different state. */
  47.     if (theRsrc != NULL) DetachResource(theRsrc);
  48.     return (CIconButtonHandle) theRsrc;
  49. }
  50.  
  51.  
  52. /* DisposeCIconButton disposes of any structures allocated
  53.     for the color icon button allocated by NewCIconButton. */
  54. void DisposeCIconButton(CIconButtonHandle cicb) {
  55.     DisposeHandle((Handle) cicb);
  56. }
  57.  
  58.  
  59.  
  60. /* GetCIconButtonStringData returns a new handled containing
  61.     the string data copied from the color icon resource.  The
  62.     handle will contain a C-style string terminated with a zero
  63.     byte.  It is the caller's responsibility to dispose of this
  64.     handle after it has been used. */
  65. OSErr GetCIconButtonStringData(CIconButtonHandle cicb, Handle *strdata) {
  66.     char handstate;
  67.     OSErr err;
  68.     handstate = HGetState((Handle) cicb);
  69.     HLock((Handle) cicb);
  70.     err = PtrToHand((**cicb).stringdata, strdata, strlen((**cicb).stringdata) + 1);
  71.     HSetState((Handle) cicb, handstate);
  72.     return err;
  73. }
  74.  
  75.  
  76. /* SetCIconButtonPosition sets the color icon button's 
  77.     screen postion. h and v are coordinates in the
  78.     current grafport. */
  79. void SetCIconButtonPosition(CIconButtonHandle cicb, short h, short v) {
  80.     char handstate;
  81.     handstate = HGetState((Handle) cicb);
  82.     HLock((Handle) cicb);
  83.     OffsetRect(&(**cicb).bounds, h - (**cicb).bounds.left, v - (**cicb).bounds.top);
  84.     HSetState((Handle) cicb, handstate);
  85. }
  86.  
  87.  
  88. /* DrawCIconButton draws the icon button using the
  89.     as it should appear given the state specified.  state
  90.     should be either kCBdisabled, kCBup, or kCBdown.
  91.     The last state a button is drawn in affects the
  92.     result of TrackCIconButton. */
  93. void DrawCIconButton(CIconButtonHandle cicb, short state) {
  94.     CIconHandle theIcon;
  95.     char handstate;
  96.     handstate = HGetState((Handle) cicb);
  97.     HLock((Handle) cicb);
  98.     theIcon = GetCIcon((**cicb).cicnIDs[state]);
  99.     if (theIcon != NULL) {
  100.         PlotCIcon(&(**cicb).bounds, theIcon);
  101.         DisposeCIcon(theIcon);
  102.     }
  103.     (**cicb).drawnstate = state;
  104.     HSetState((Handle) cicb, handstate);
  105. }
  106.  
  107.  
  108. /* TrackCIconButton should be called whenever a click is made
  109.     inside of a color icon button.  if the last time the button
  110.     was drawn its state was not kCBup or the click is outside
  111.     of the button, then this routine returns false. */
  112. Boolean TrackCIconButton(CIconButtonHandle cicb, Point where) {
  113.     Boolean result;
  114.     char handstate;
  115.     handstate = HGetState((Handle) cicb);
  116.     HLock((Handle) cicb);
  117.     if ((**cicb).drawnstate == kCBdisabled) {
  118.         result = false;
  119.     } else if (PtInRect(where, &(**cicb).bounds)) {
  120.         Boolean isin, ishilited;
  121.         Point mouseLoc;
  122.         isin = true;
  123.         ishilited = true;
  124.         mouseLoc = where;
  125.         DrawCIconButton(cicb, kCBdown);
  126.         while (StillDown()) {
  127.             GetMouse(&mouseLoc);
  128.             isin = PtInRect(mouseLoc, &(**cicb).bounds);
  129.             if (isin != ishilited) {
  130.                 ishilited = isin;
  131.                 DrawCIconButton(cicb, ishilited ? kCBdown : kCBup);
  132.             }
  133.         }
  134.         result = ishilited;
  135.     } else result = false;
  136.     HSetState((Handle) cicb, handstate);
  137.     return result;
  138. }
  139.